Skip to content

feat(backend): role-based control guards for route scopes#328

Open
armorbreak001 wants to merge 1 commit intoGalactiGuild:mainfrom
armorbreak001:bounty/296-role-based-guards
Open

feat(backend): role-based control guards for route scopes#328
armorbreak001 wants to merge 1 commit intoGalactiGuild:mainfrom
armorbreak001:bounty/296-role-based-guards

Conversation

@armorbreak001
Copy link
Copy Markdown

Fixes #296

Summary

Implemented role-based access control (RBAC) guards for NestJS routes, with @Roles() decorator and RolesGuard. Applied to restrict bounty mutation endpoints to ADMIN users.

Changes

New Files

  • backend/src/common/decorators/roles.decorator.ts@Roles(...roles) metadata decorator
  • backend/src/common/guards/roles.guard.tsRolesGuard that:
    • Reads required roles from handler/class metadata via Reflector
    • Checks user.roles (supports string[] or comma-separated string)
    • Allows access if user has any of the required roles
    • Returns 403 Forbidden with descriptive message on denial
    • Passes through when no roles are required
  • backend/src/common/guards/roles.guard.spec.ts — Unit tests (4 cases)

Modified

  • backend/src/bounty/bounty.controller.ts — Added RolesGuard + @Roles(ADMIN) to PATCH /bounties/:id

Usage

// Single role
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(ADMIN)
@Patch(:id)
async update(...) { }

// Multiple roles (user needs at least one)
@Roles(ADMIN, MODERATOR)

Verification

# Run guard tests
cd backend && npm test -- common/guards/roles.guard.spec.ts

# Manual test:
# PATCH /bounties/1 with user.role=USER → 403 Forbidden
# PATCH /bounties/1 with user.role=ADMIN → 200 OK

- Add @roles() decorator for setting required roles on routes
- Add RolesGuard checking user.roles against required roles
- Apply RolesGuard to PATCH /bounties/:id (ADMIN only)
- Returns 403 Forbidden with descriptive message on denial
- Includes unit tests for RolesGuard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Backend] Role-Based Control Guards for Route Scopes

1 participant